package com.gutai.admin.service.impl;

import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.domain.Sort;
import org.springframework.data.domain.Sort.Direction;
import org.springframework.stereotype.Service;

import com.gutai.admin.dao.IGoodsSpecificationDao;
import com.gutai.admin.dao.IProductDao;
import com.gutai.admin.entity.Goods;
import com.gutai.admin.entity.GoodsSpecification;
import com.gutai.admin.service.IGoodsService;
import com.gutai.admin.service.IGoodsSpecificationService;

/**
 * 
 * 描述：
 * 
 * <pre>
 * HISTORY
 * ****************************************************************************
 *  ID    DATE            PERSON            REASON
 *  1     2018年11月7日         YC           Create
 * ****************************************************************************
 * </pre>
 * 
 * @author YC
 * @version 1.0
 */
@Service
@CacheConfig(cacheNames = "goodsSpecification")
public class GoodsSpecificationService implements IGoodsSpecificationService {

    @Autowired
    private IGoodsSpecificationDao goodsSpecificationDao;
    @Autowired
    private IProductDao productDao;
    @Autowired
    private IGoodsService goodsService;


    @Override
    public List<GoodsSpecification> getAll() {
        Sort sort = new Sort(Direction.DESC, "createTime");
        return goodsSpecificationDao.findAll(sort);
    }

    @Override
    @CacheEvict(allEntries = true)
    public void add(GoodsSpecification entity) {
        // entity.setId(IdGeneratorUtil.generate());
        goodsSpecificationDao.save(entity);

//        Map<String, Object> condsMap = new HashMap<>();
//        condsMap.put("goods_id", entity.getGoodsId());
//        condsMap.put("specification_id", entity.getSpecificationId());
//        List<GoodsSpecification> entityAddeds = goodsSpecificationDao.queryByConds(GoodsSpecification.class, condsMap);
//        GoodsSpecification entityAdded = entityAddeds.get(0);
//        
//        Map<String, Object> condsMap2 = new HashMap<>();
//        condsMap2.put("goods_id", entity.getGoodsId());
//        List<Product> products = productDao.queryByConds(Product.class, condsMap2);
//        Product product = products.get(0);
//        
//        String spIds = product.getGoodsSpecificationIds();
//        if (StringUtils.isNotBlank(spIds)){
//            spIds += "_"+entityAdded.getId();
//        } else {
//            spIds =String.valueOf(entityAdded.getId());
//        }
//        product.setGoodsSpecificationIds(spIds);
//        return productDao.update(product);
    }

    @Override
    @CacheEvict(allEntries = true)
    public void update(GoodsSpecification entity) {
        goodsSpecificationDao.saveAndFlush(entity);
    }

    @Override
    @CacheEvict(allEntries = true)
    public void delete(int id) {
        goodsSpecificationDao.deleteById(id);
    }

    @Override
    public GoodsSpecification findById(int id) {
        return goodsSpecificationDao.findById(id);
    }

    @Override
    @Cacheable(key = "'goods$'+#goodsId")
    public List<GoodsSpecification> findByGoodsId(int goodsId) {
        return goodsSpecificationDao.findByGoodsId(goodsId).stream()
                .sorted(Comparator.comparingLong(GoodsSpecification::getCreateTime).reversed())
                .collect(Collectors.toList());
    }

    @Override
    public List<GoodsSpecification> findByGoodsName(String goodsName) {
        List<Integer> goodsIds = goodsService.findByName(goodsName).stream().map(Goods::getId).collect(Collectors.toList());
        return goodsIds.stream().flatMap(goodsId->findByGoodsId(goodsId).stream()).collect(Collectors.toList());
    }
}
